﻿using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Caching;


namespace Treasure.Service
{
   
    public class CacheHelps
    {
        private static int _cacheUpdate = 30; //默认缓存时间
        private static volatile Cache webCache = HttpRuntime.Cache;

        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Get(string key)
        {
            return webCache.Get(key);
        }
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key)
        {
            if (webCache.Get(key) == null)
                return default(T);
            return (T)webCache.Get(key);
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值 ,绝对过期30分钟
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Insert(string key, object value)
        {
            Insert(key, value, CacheUpdate);
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值,缓存时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="Minutes"></param>
        public static void Insert(string key, object value, int Minutes)
        {
            if (value != null)
            {
                webCache.Insert(key, value, null, DateTime.Now.AddSeconds((double)Minutes), Cache.NoSlidingExpiration);
            }
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值,缓存时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="Minutes"></param>
        public static void Insert<T>(string key, T value, int Minutes)
        {
            if (value != null)
            {
                webCache.Insert(key, value, null, DateTime.Now.AddSeconds((double)Minutes), Cache.NoSlidingExpiration);
            }
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值,滑动过期时间（分钟）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="Minutes"></param>
        public static void InsertSpan(string key, object value, int Minutes)
        {
            if (value != null)
            {
                webCache.Insert(key, value, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, Minutes,0));
            }
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值,滑动过期时间（分钟）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="Minutes"></param>
        public static void InsertSpan<T>(string key, T value, int Minutes=30)
        {
            if (value != null)
            {
                webCache.Insert(key, value, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, Minutes, 0));
            }
        }




        /// <summary>
        /// 删除KEY缓存
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            webCache.Remove(key);
        }
        /// <summary>
        /// 清除缓存
        /// </summary>
        public static void Clear()
        {
            IDictionaryEnumerator enumerator = webCache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                webCache.Remove(enumerator.Key.ToString());
            }
        }
        /// <summary>
        /// 缓存默认时间
        /// </summary>
        public static int CacheUpdate
        {
            get
            {
                return _cacheUpdate;
            }
            set
            {
                _cacheUpdate = value;
            }
        }

        public static Cache WebCache
        {
            get
            {
                HttpContext current = HttpContext.Current;
                if (current != null)
                {
                    webCache = current.Cache;
                }
                else
                {
                    webCache = HttpRuntime.Cache;
                }
                return webCache;
            }
        }
    }
}

